package com.jd.system.controller;

import com.jd.basic.util.AjaxResult;
import com.jd.basic.util.PageList;
import com.jd.system.domain.Dictionarydetail;
import com.jd.system.service.DictionarydetailService;
import com.jd.system.query.DictionarydetailQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dictionarydetail")
public class DictionarydetailController {

    @Autowired
    private DictionarydetailService dictionarydetailService;

    @GetMapping
    public List<Dictionarydetail> loadAll() {
        return dictionarydetailService.loadAll();
    }

    @GetMapping("/{id}")
    public Dictionarydetail loadById(@PathVariable("id")Long id) {
        return dictionarydetailService.loadById(id);
    }

    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id")Long id) {
        try {
            dictionarydetailService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }

    }

    @PutMapping
    public AjaxResult addUpdate(@RequestBody Dictionarydetail dictionarydetail) {
        try {
            if (dictionarydetail.getId() == null) {
                dictionarydetailService.save(dictionarydetail);
            }else {
                dictionarydetailService.update(dictionarydetail);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }

    }

    @PatchMapping
    public AjaxResult patchRemove(@RequestBody List<Long> ids) {
        try {
            dictionarydetailService.patchRemove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

   @PostMapping
    public PageList<Dictionarydetail> find(@RequestBody DictionarydetailQuery dictionarydetailQuery) {
        return dictionarydetailService.queryPage(dictionarydetailQuery);
    }

}
